package com.example.seh.util;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.seh.chat.Msg;
import com.example.seh.constant.Constant;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer extends Thread {
    public ServerSocket server=null;
    private   int PORT =5000;
    private BufferedWriter writer;
    private BufferedReader reader;
    public   ChatServer() throws IOException {
            createSocket();
    }
    public void run(){
        Socket client = null;
        String txt = null;
        Log.e("chat","成功开始服务器，端口为："+PORT);
        try{
            while (true){
                client=ResponseSocket();
                while(true){
                    Log.e("chat","等待数据ing");
                    txt=ReceiveMsg(client);
                    System.out.println(txt);
                    Log.e("chat","接收到的内容:" + txt);
                    if (txt != null) {
                        Message message1 = new Message();
                        message1.what = 2;
                        message1.obj =txt;
                        if (Constant.isChat) {
                            if (!ImgUtil.isImg(txt)) {
                                Log.e("chat","接收到的是文本" + txt);
                                ChatUtil.msgList.add(new Msg(UserMsg.fromId, UserMsg.userDTO.getUserId(), txt, null, Msg.MSG));
                            } else {
                                Log.e("chat","接收到的是图片" + txt);
                                ChatUtil.msgList.add(new Msg(UserMsg.fromId, UserMsg.userDTO.getUserId(), null, txt, Msg.IMG));
                            }
                            com.example.seh.chat.ChatActivity.handler.sendMessage(message1);

                        }else {
//                        ChatUtil.msgList.add(new Msg(txt, null, Msg.TYPE_RECEIVED, Msg.MSG));
                            if (!ImgUtil.isImg(txt)) {
                                ChatUtil.msgCache.add(new Msg(UserMsg.fromId, UserMsg.userDTO.getUserId(), txt, null, Msg.MSG));
                            } else {
                                ChatUtil.msgCache.add(new Msg(UserMsg.fromId, UserMsg.userDTO.getUserId(), null, txt, Msg.IMG));
                            }

                        }
                    }

                    if(true)
                        break;
                }
//                CloseSocket(client);
            }
        }catch (IOException e){

        }
    }
//    private Handler mHandler = new Handler(){
//        public void handleMessage(Message msg) {
//
//        };
//    };


    public void createSocket() throws IOException{
        server=new ServerSocket(PORT,100);
    }

    public Socket ResponseSocket() throws IOException{
        Socket client =server.accept();
        return client;
    }

    public void CloseSocket(Socket socket) throws IOException{
        reader.close();
        socket.close();
        server.close();
    }

    public void sendMsg(Socket socket,String string)throws IOException{
        writer=new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream())
        );
        writer.write("msg"+string);
        writer.flush();
    }

    public String ReceiveMsg(Socket socket) throws IOException{
        reader=new BufferedReader(
                new InputStreamReader(socket.getInputStream())
        );
                String txt =reader.readLine();
                Log.e("chat","reader:" + txt);
        return txt;
    }

}